<template>
  <div class="equipment-emission">
    <div class="section-title">
      <h3>{{ usagePhase.replace("阶段", "设备") }}产生的碳排放</h3>
      <div class="toolbar">
        <el-button type="success" size="small" @click="addEquipment" icon="el-icon-plus">新增设备</el-button>
        <el-button type="primary" size="small" @click="batchEdit" icon="el-icon-check">保存修改</el-button>
        <el-button type="danger" size="small" @click="clearAll" icon="el-icon-delete">清空设备</el-button>
      </div>
    </div>

    <el-table :data="equipmentData" style="width: 100%" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55"></el-table-column>
      <el-table-column label="序号" width="80">
        <template slot-scope="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column label="设备名称" prop="equipmentName">
        <template slot-scope="scope">
          <el-input v-model="scope.row.equipmentName" size="small" placeholder="请输入设备名称" />
        </template>
      </el-table-column>
      <el-table-column label="台班" prop="shiftCount">
        <template slot-scope="scope">
          <el-input-number v-model="scope.row.shiftCount" size="small" :min="0" :controls="false" />
        </template>
      </el-table-column>
      <el-table-column label="单位" prop="unit">
        <template slot-scope="scope">
          {{ scope.row.unit }}
        </template>
      </el-table-column>
      <el-table-column label="碳排放因子(kg/台班)" prop="carbonFactor">
        <template slot-scope="scope">
          <el-input-number v-model="scope.row.carbonFactor" size="small" :min="0" :precision="2" :controls="false" />
        </template>
      </el-table-column>
      <el-table-column label="碳排放量" prop="emission">
        <template slot-scope="scope">
          {{ (scope.row.shiftCount * scope.row.carbonFactor).toFixed(2) }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="100">
        <template slot-scope="scope">
          <el-button size="small" type="danger" @click="removeEquipment(scope.row.equipmentInfoId)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
  </div>
</template>

<script>
import { updateEquipmentInfo, delEquipmentInfo } from "@/api/business/equipmentInfo"

export default {
  props: {
    equipmentData: {
      type: Array,
      default: () => []
    },
    usagePhase: {
      type: String,
      default: ""
    },
  },

  data() {
    return {
      selectedEquipment: []
    }
  },
  created() {
  },

  watch: {
  },

  methods: {
    addEquipment() {
      // 通知父组件打开窗口
      this.$emit('openEquipmentDialog')
    },

    removeEquipment(equipmentInfoId) {
      // 删除设备
      const equipmentInfoIds = equipmentInfoId
      this.$modal.confirm('是否确认删除设备信息编号为"' + equipmentInfoIds + '"的数据项？').then(function() {
        return delEquipmentInfo(equipmentInfoIds)
      }).then(() => {
        this.$emit('refreshEquipmentInfo')
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },

    batchEdit() {
      // 生成所有请求的 Promise 数组
      const promises = this.equipmentData.map(item =>
        updateEquipmentInfo(item.equipmentInfoId, item)
      );

      // 等待所有请求完成
      Promise.all(promises)
        .then(() => {
          this.$modal.msgSuccess("全部修改成功");
          this.$emit('refreshEquipmentInfo');
        })
    },

    clearAll() {
      // 获取所有id，删除设备
      const equipmentInfoIds = this.equipmentData.map((item) => item.equipmentInfoId)
      this.$modal.confirm('是否确认删除设备信息编号为"' + equipmentInfoIds + '"的数据项？').then(function() {
        return delEquipmentInfo(equipmentInfoIds)
      }).then(() => {
        this.$emit('refreshEquipmentInfo')
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },

    handleSelectionChange(selection) {
      this.selectedEquipment = selection
    }
  }
}
</script>

<style scoped>
.equipment-emission {
  margin-bottom: 24px;
  padding: 20px;
  background: #fff;
  border-radius: 4px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
}

.section-title {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 20px;
}

.toolbar {
  display: flex;
  gap: 10px;
}
</style>
